package stockblaster.client;

import java.util.ArrayList;

import stockblaster.gen.R;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchAdapter extends BaseAdapter {
	Activity activity;
    ArrayList<CompanySearchViewData> items = null;
    boolean isPortfolio = false;

    public SearchAdapter(Activity activity,
    		ArrayList<CompanySearchViewData> items, boolean _isPortfolio) {
            super();
            this.activity = activity;
            this.items = items;
            this.isPortfolio = _isPortfolio;
    }

	public int getCount() {
		return items.size();
	}

	public Object getItem(int arg0) {
		return items.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}
	
	 private class ViewHolder {
        	TextView cName, cGrowth, sPrice, sOwned, tValue, gainLoss;
        	ImageView cLogo;
        	ImageView cArrow;
        }
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = new ViewHolder();
		LayoutInflater vi = activity.getLayoutInflater();

		if (convertView == null) {
			if (isPortfolio) {
				convertView = vi.inflate(R.layout.portfolio_row_view, null);
			} else {
				convertView = vi.inflate(R.layout.search_row_view, null);
			}
			vh.cName = (TextView) convertView.findViewById(R.id.textView1);
			vh.cGrowth = (TextView) convertView.findViewById(R.id.textView2);
			vh.sPrice = (TextView) convertView.findViewById(R.id.textView3);
			vh.sOwned = (TextView) convertView.findViewById(R.id.textView4);
			vh.cLogo = (ImageView) convertView.findViewById(R.id.imageView1);
			vh.cArrow = (ImageView) convertView.findViewById(R.id.imageView2);
			if (isPortfolio) {
				vh.tValue = (TextView) convertView.findViewById(R.id.textView5);
				vh.gainLoss = (TextView) convertView
						.findViewById(R.id.textView10);
			}
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		CompanySearchViewData csvd = items.get(position);
		vh.cName.setText(csvd.getName());
		vh.cGrowth.setText(csvd.getGrowth() + "%");
		vh.sPrice.setText(MethodHelper.twoDecimal(csvd.getPrice()));
		vh.sOwned.setText("Shares Owned: " + csvd.getOwned());
		vh.cLogo.setBackgroundResource(csvd.getLogo());
		vh.cArrow.setBackgroundResource(csvd.getArrow());
		if (csvd.getArrow() == R.drawable.stock_up) {
			vh.cGrowth.setTextColor(Color.parseColor("#00FF00"));
		} else if (csvd.getArrow() == R.drawable.stock_down) {
			vh.cGrowth.setTextColor(Color.RED);
		}
		if (isPortfolio) {
			vh.tValue.setText("Total Value: "
					+ MethodHelper.twoDecimal(csvd.getUserTotalValue()));
			if (CurrentUser.getMonth() > 1) {
				vh.gainLoss.setText(MethodHelper.twoDecimal(Math.abs(csvd
						.getGainLoss())));
				if (csvd.getGainLoss() > 0) {
					vh.gainLoss.setTextColor(Color.parseColor("#00FF00"));
				} else if (csvd.getGainLoss() < 0) {
					vh.gainLoss.setTextColor(Color.RED);
				} else if (csvd.getGainLoss() == 0) {
					vh.gainLoss.setTextColor(Color.WHITE);
				}
			} else {
				vh.gainLoss.setText("$0.00");
				vh.gainLoss.setTextColor(Color.WHITE);
			}
		}
		return convertView;
	}
}
